﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace HodhodNews.Extraction
{
    public static class ExtensionMethods
    {
        public static bool EqualsAny(this string me, params string[] strs)
        {
            foreach (string item in strs)
            {
                if (me == item)
                    return true;
            }
            return false;
        }

        public static string TrySubstractWords(this string text, int startIndex, int wordsCount)
        {
            if (string.IsNullOrEmpty(text) || text.Trim().Length == 0)
                return "";

            string substractedText = null;
            int counter = 0;

            int maxLength = text.Length;
            if (maxLength > startIndex)
            {
                string splitter = GetSplitter(text);
                string word = GetNextWord(text, startIndex);
                if (word == null)
                {
                    return substractedText;
                }
                else
                {
                    do
                    {
                        substractedText += word + splitter;
                        word = GetNextWord(text, substractedText.Length);
                        counter++;

                    } while (word != null && counter <= wordsCount);
                }
            }
            return substractedText;
        }
        static string GetSplitter(string text)
        {
            if (text.IndexOf(" ") != -1)
                return " ";
            else return "&nbsp;";
        }
        static string GetNextWord(string text, int startIndex)
        {
            string spiltter = GetSplitter(text);
            int nextWordIndex = -1;
            int nextWordLength = 0;

            nextWordIndex = text.IndexOf(spiltter, startIndex);
            if (nextWordIndex == -1)
            {
                return null;
            }
            else
            {
                nextWordLength = nextWordIndex - startIndex;
                return text.Substring(startIndex, nextWordLength);
            }
        }
    }
}
